package gui;

import AI.*;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Runnable.*;

import database.*;
import java.io.File;
import java.util.ArrayList;

/**
 *
 * @author Scott
 */
public class GUI extends JFrame{

    /**
     * @param args the command line arguments
     */

    JFrame mainFrame = new JFrame();
    JPanel buttonPanel = new JPanel();
    static Database db;


    public GUI(){

        db = new Database();
         
    }
    
    public void initComponents()
    {
        createMenuBar();

        mainFrame.setTitle("AutoMatic Notes, Inc");
        mainFrame.setLayout(new BorderLayout());
        
        JButton viewLectureButton = new JButton();
        viewLectureButton.setLabel("View Lecture");
        viewLectureButton.addActionListener(new viewLectureListener());

        //this appears for all users
       // buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        buttonPanel.add(viewLectureButton);

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public JFrame getMainFrame(){
        return mainFrame;
    }

    public void runAdminUI(JFrame main){

        AdminUI newAdminUI = new AdminUI(main);
        newAdminUI.drawAdminComponents();
        
    }

    public void runInstructorUI(JFrame main){
        InstructorUI newInstructorUI = new InstructorUI(main);
        newInstructorUI.drawInstructorComponents();

    }

    public void runStudentUI(JFrame main){

    }

    public void createMenuBar(){
        
        //create the menubar
        JMenuBar menuBar = new JMenuBar();
       
        //create menus
        JMenu fileMenu = new JMenu("File");

        //create menu items
        JMenuItem viewItem = new JMenuItem("View Lecture");
        JMenuItem quitItem = new JMenuItem("Quit");

        //give menu functionality
        quitItem.addActionListener(new quitListener());
        viewItem.addActionListener(new viewLectureListener());

        //add components to menu
        menuBar.add(fileMenu);
        fileMenu.add(viewItem);
        fileMenu.add(quitItem);
        mainFrame.setJMenuBar(menuBar);
    }

    private class viewLectureListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser(".");
                File f = null;
                int status = chooser.showOpenDialog(null);
                if (status == JFileChooser.APPROVE_OPTION)
                    f = chooser.getSelectedFile();
                LessonDecoder dec = new LessonDecoder(f);
                ArrayList<topics.MainTopic> tops = dec.decode();

                DrawTree tree = new DrawTree(tops);

                SequentialLesson seq = new SequentialLesson(tops);

                PuzzleLesson p = new PuzzleLesson(tops);
                
        }

    }

    private class quitListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            db.writeToFile();
            System.exit(0);
        }

    }
}
